//
//  TAPageControl+Style.m
//  HuaXin
//
//  Created by 江伟 on 2018/11/4.
//

#import "TAPageControl+Style.h"
#import "TAAbstractDotView.h"
@implementation TAPageControl (Style)

static NSString *styleKey = @"style";

- (void)changeActivity:(BOOL)active atIndex:(NSInteger)index
{
    if (self.pageStyle == 100) {
        [self none_changeActivity:active atIndex:index];
        return;
    }
    
    NSArray *dots = [self valueForKey:@"dots"];
    for (int i = 0; i < dots.count; i ++) {
        UIImageView *dotView = (UIImageView *)[dots objectAtIndex:i];
        CGPoint center = dotView.center;
        if (self.pageStyle == 0) {
            dotView.width = 7;
            dotView.height = 7;
        } else if (self.pageStyle == 1) {
            dotView.width = 5;
            dotView.height = 3;
        }
        dotView.center = center;
    }
    
    if (self.dotViewClass) {
        TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[dots objectAtIndex:index];
        if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
            [abstractDotView changeActivityState:active];
            CGPoint center = abstractDotView.center;
            if (self.pageStyle == 0) {
                abstractDotView.width = 13;
                abstractDotView.height = 7;
            } else if (self.pageStyle == 1) {
                abstractDotView.width = 8;
                abstractDotView.height = 3;
            }
            abstractDotView.center = center;
        } else {
            NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
        }
    } else if (self.dotImage && self.currentDotImage) {
        UIImageView *dotView = (UIImageView *)[dots objectAtIndex:index];
        dotView.image = (active) ? self.currentDotImage : self.dotImage;
        CGPoint center = dotView.center;
        if (self.pageStyle == 0) {
            dotView.width = 13;
            dotView.height = 7;
        } else if (self.pageStyle == 1) {
            dotView.width = 8;
            dotView.height = 3;
        }
        dotView.center = center;
    }
}

- (void)none_changeActivity:(BOOL)active atIndex:(NSInteger)index
{
    NSArray *dots = [self valueForKey:@"dots"];
    if (self.dotViewClass) {
        TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[dots objectAtIndex:index];
        if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
            [abstractDotView changeActivityState:active];
        } else {
            NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
        }
    } else if (self.dotImage && self.currentDotImage) {
        UIImageView *dotView = (UIImageView *)[dots objectAtIndex:index];
        dotView.image = (active) ? self.currentDotImage : self.dotImage;
    }
}

- (void)setPageStyle:(TAPageControlStyle)pageStyle {
    objc_setAssociatedObject(self, &styleKey, @(pageStyle), OBJC_ASSOCIATION_ASSIGN);
}

- (TAPageControlStyle)pageStyle {
    NSNumber *number = objc_getAssociatedObject(self, &styleKey);
    NSInteger index = [number integerValue];
    return index;
}

@end
